package com.yan.flower.service;

import com.yan.flower.dao.Dao;
import com.yan.flower.domain.FlowerOrder;

import java.util.Scanner;

public class FlowerServicelmpl implements FlowerService {
    public static final Scanner INPUT = new Scanner(System.in);

    @Override
    public void findAll() {
        System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
        for (int i = 0; i < Dao.COUNT; i++) {
            System.out.println("" + Dao.orderList[i].getId() + "\t" +  Dao.orderList[i].getName() + "\t" +  Dao.orderList[i].getNum() + "\t\t" +  Dao.orderList[i].getPrice() + "\t" +  Dao.orderList[i].getDate() + "\t" +  Dao.orderList[i].getSalesMan() + "\t" +  Dao.orderList[i].getRemark() + "");
        }
        System.out.println();
        System.out.println();
    }

    @Override
    public void update() {
        System.out.println("输入销售编号");
        String uid = INPUT.next();
        boolean flag = false;
        for (FlowerOrder order :  Dao.orderList) {
            if (order.getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            for (int i = 0; i <  Dao.orderList.length; i++) {
                if ( Dao.orderList[i].getId().equals(uid)) {
                    System.out.println("您要修改的订单信息如下");
                    System.out.println();
                    System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
                    System.out.println("" +  Dao.orderList[i].getId() + "\t" +  Dao.orderList[i].getName() + "\t" +  Dao.orderList[i].getNum() + "\t\t" +  Dao.orderList[i].getPrice() + "\t" +  Dao.orderList[i].getDate() + "\t" +  Dao.orderList[i].getSalesMan() + "\t" +  Dao.orderList[i].getRemark() + "");
                    System.out.println("请输入修改后的鲜花名称");
                    String newName = INPUT.next();
                    System.out.println("请输入修改后的销售数量");
                    int newNum = INPUT.nextInt();
                    System.out.println("请输入修改后的鲜花价格");
                    int newPrice = INPUT.nextInt();
                    System.out.println("请输入修改后的销售日期(yyyy-MM-dd)");
                    String newDate = INPUT.next();
                    System.out.println("请输入修改后的销售员");
                    String newSale = INPUT.next();
                    System.out.println("请输入修改后的备注");
                    String newRemark = INPUT.next();
                    System.out.println("您确定要修改吗（y/n）");
                    String flag1 = INPUT.next();
                    if ("y".equals(flag1)) {
                        Dao.orderList[i] = new FlowerOrder(uid, newName, newNum, newPrice, newDate, newSale, newRemark);
                        System.out.println("修改成功");
                        break;
                    }
                }
            }

        } else {
            System.out.println("对不起,您输入的id不正确");
        }
    }

    @Override
    public void delete() {
        System.out.println("请输入要删出的订单号");
        String uid = INPUT.next();
        boolean flag = false;
        for (FlowerOrder order :  Dao.orderList) {
            if (order.getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            for (int i = 0; i <  Dao.orderList.length; i++) {
                if ( Dao.orderList[i].getId().equals(uid)) {
                    System.out.println("您要删除的订单信息如下");
                    System.out.println();
                    System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
                    System.out.println("" +  Dao.orderList[i].getId() + "\t" +  Dao.orderList[i].getName() + "\t" +  Dao.orderList[i].getNum() + "\t\t" +  Dao.orderList[i].getPrice() + "\t" +  Dao.orderList[i].getDate() + "\t" +  Dao.orderList[i].getSalesMan() + "\t" +  Dao.orderList[i].getRemark() + "");
                    System.out.println("您确定要删除吗?(y/n)");
                    String flag1 = INPUT.next();
                    if ("y".equals(flag1)) {
                        for (int j = i; j < Dao.COUNT - 1; j++) {
                            Dao.orderList[j] =  Dao.orderList[j + 1];
                        }
                    }
                }
            }
            System.out.println("删除成功！");
            Dao.COUNT--;
        } else {
            System.out.println("对不起,您输入的编号不正确");
        }

    }
}
